define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consult/reply/index' + location.search,
                    add_url: 'consult/reply/add',
                    edit_url: 'consult/reply/edit',
                    del_url: 'consult/reply/del',
                    multi_url: 'consult/reply/multi',
                    table: 'consult_reply',
                }
            });

            var table = $("#table");
            var  stvisible = '=';
            var  stvisible2 = true;
            var  stvisible3 = false;
            if(Config.org_cate_id > 0){
                stvisible = false;
                stvisible2 = false;
                stvisible3 = true;
            }
            table.on('post-common-search.bs.table', function (event, table) {
                $(".commonsearch-table").find('.btn-success').html('搜索');
                $(".commonsearch-table").find('.btn-default').html('刷新');
                $(".columns-right").hide();
                $('.form-commonsearch').attr('autocomplete', 'off');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'sort', title: __('序号'), operate:false},
                        {field: 'o.name', title: __('单位名称'), visible: stvisible2, operate:stvisible, searchList: $.getJSON('usechart/searchlist')},
                        {field: 'consultroom.name', title: __('咨询室名称'),searchList:$.getJSON('consult/reply/searchlist'), formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('申请单时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'name', title: __('Name'), operate:false},
                        {field: 'mobile', title: __('Mobile'), operate:false},
                        {field: 'introduce', title: __('Introduce'), operate:false,events: Controller.api.events.browser,formatter: Controller.api.formatter.browser},
                        {field: 'has_teacher', title: __('是否已指定老师'), operate:false, searchList: {"1":__('已指定'),"2":__('未指定')}, formatter: Table.api.formatter.normal},
                        {field: 'start_time', title: __('咨询开始时间'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'ent_time', title: __('咨询结束时间'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'type', title: __('申请单状态'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3'),"4":__('Type 4')}, formatter: Table.api.formatter.normal},
                        // {field: 'teacher_name', title: __('Learncateteacher.name'), operate:false},
                        {field: 'operate', title: __('Operate'), visible:stvisible3, table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                // {
                                //     name: 'chakan',
                                //     title: __('查看'),
                                //     text:'查看',
                                //     classname: 'btn btn-xs btn-success btn-click',
                                //     // icon: 'fa fa-pencil',
                                //     click: function (data,row) {
                                //         chakan(row.id);
                                //         // Layer.alert("点击按钮执行的事件");
                                //     }
                                // },

                                {
                                    name: 'teacher',
                                    title: __('设置老师'),
                                    text:'设置老师',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    click: function (data,row) {
                                        setteacher(row.id);
                                        // Layer.alert("点击按钮执行的事件");
                                    }
                                },
                                // {
                                //     name: 'complete',
                                //     title: __('完成咨询'),
                                //     text:'完成咨询',
                                //     classname: 'btn btn-xs btn-warning btn-click',
                                //     click: function (data,row) {
                                //         confirmf(row.id);
                                //         // Layer.alert("点击按钮执行的事件");
                                //     }
                                // },
                            ]
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                search:false,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            var confirmf = function(id){
                Layer.confirm("确定设置成完成状态吗？",
                    {
                        title: '完成咨询',
                        btn: ["确定", "取消"],
                        yes: function (index, layero) {
                            submitExportForm(id,index, layero);
                            return false;
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                            return false;
                        },
                    });
            }
            var submitExportForm = function (id,index, layero) {
                var data = {
                    ids: id,
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('consult/reply/complete'),
                    data: data,
                    pk: 'id'
                }, function (res) {
                    Layer.closeAll();
                    Fast.api.close(layero.selector);
                    $(".btn-refresh").trigger("click");
                });
            };
            var chakan = function (id) {
                Fast.api.open('consult/reply/chakan?ids='+id, '查看', {
                    area:["100%","100%"],
                    callback:function(){
                        Fast.api.close();
                        $(".btn-refresh").trigger('click');//刷新当前页面的数据
                    }
                })
            };
            var setteacher = function (id) {
                Fast.api.open('consult/reply/teacher?ids='+id, '设置老师', {
                    area:["100%","100%"],
                    callback:function(){
                        Fast.api.close();
                        $(".btn-refresh").trigger('click');//刷新当前页面的数据
                    }
                })
            };
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        chakan: function () {
            Controller.api.bindevent();
        },
        teacher: function () {
            Controller.api.bindevent();
        },
        complete: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return row.oname;
                },
                browser: function (value, row, index) {
                    //这里我们直接使用row的数据
                    return '<a class="btn btn-xs btn-browser">' + row.introduce+ '</a>';
                },
            },
            events: {//绑定事件的方法
                browser: {
                    'click .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert("<code>" + JSON.stringify(row.introduce) + "</code>");
                    }
                },
            }
        }
    };
    return Controller;

});